package com.hvisions.service.impl;

import com.hvisions.repository.PermissionRepository;
import com.hvisions.entity.Permission;
import com.hvisions.service.IPermissionService;
import com.hvisions.util.CopyUtil;
import com.hvisions.util.CoreConst;
import com.hvisions.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @Author: Lau
 * @Description:
 * @Date: Created in 11:40 2019/10/24
 * @Modified By:
 */
@Service
public class PermissionServiceImpl implements IPermissionService {


    @Autowired
    PermissionRepository permissionRepository;

    @Override
    public Set<String> findPermsByUserId(String userId) {
        return permissionRepository.findPermsByUserId(userId);
    }

    @Override
    public List<Permission> selectAll(Integer status) {
        return permissionRepository.findAllByStatusOrderByOrderNum(status);
    }

    @Override
    public List<Permission> selectAllMenuName(Integer status) {
        return permissionRepository.findAllByStatusOrderByOrderNum(status);
    }

    @Override
    public List<Permission> selectMenuByUserId(String userId) {
        return permissionRepository.selectMenuByUserId(userId);
    }

    @Override
    public void insert(Permission permission) {
        Date date = new Date();
        permission.setPermissionId(UUIDUtil.getUniqueIdByUUId());
        permission.setStatus(CoreConst.STATUS_VALID);
        permission.setCreateTime(date);
        permission.setUpdateTime(date);
        permissionRepository.save(permission);

    }

    @Override
    public int updateStatus(String permissionId, Integer status) {
        return permissionRepository.updateStatusByPermissionId(permissionId, status);
    }

    @Override
    public Permission findByPermissionId(String permissionId) {
        return permissionRepository.findByPermissionId(permissionId);
    }

    @Override
    public Permission findById(Integer id) {

        return permissionRepository.getOne(id);

    }

    @Override
    public int updateByPermissionId(Permission permission) {
        permission.setUpdateTime(new Date());
        Permission byPermissionId = permissionRepository.findByPermissionId(permission.getPermissionId());
        CopyUtil.copyNotNull(permission, byPermissionId);
        Permission save = permissionRepository.save(byPermissionId);

        return save.getId();
    }

    @Override
    public int selectSubPermsByPermissionId(String permissionId) {
        return permissionRepository.selectSubPermsByPermissionId(permissionId);
    }

}
